package net.trustie.webmagic.one;

import net.trustie.webmagic.extrator.CSSPathExtractor;
import net.trustie.webmagic.extrator.Extractor;
import net.trustie.webmagic.extrator.URLPatternExtractor;
import net.trustie.webmagic.one.dao.ErrorPageDao;
import net.trustie.webmagic.one.dao.ListHtmlDao;
import net.trustie.webmagic.one.dao.PointerDAO;
import net.trustie.webmagic.one.dao.URLDao;
import net.trustie.webmagic.one.model.DetailHtml;
import net.trustie.webmagic.one.model.ListHtml;
import net.trustie.webmagic.utils.FileReader;
import net.trustie.webmagic.utils.TableHelper;
import net.trustie.webmagic.utils.Utils;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.annotation.Resource;



/**
 * Created by gyiang on 2014/12/19.
 */
public class ListHtmlProcessor implements PageProcessor {

	ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
			"classpath:/spring/applicationContext*.xml");
	
	private Site site;
//	private String conf;
		
	Extractor extractor = null;
	
	Logger logger = Logger.getLogger(this.getClass());
	private static int batchSize = 10;
//	@Resource
//	private ListHtmlDao listHtmlDao;
	@Resource
	private PointerDAO pointerDao;
	@Resource
	private URLDao urlDao;
	@Resource
	private ErrorPageDao errorPageDao;

	@Qualifier("tableHelper")
	@Autowired
	private TableHelper tableHelper;

	private String pointersTableName = "pointers";
	private String pointersSql = "";

	private String domain = "";
	private String urlTableName = "";
	private String urlTableSql = "";
	private String errorPageTableName = "";
	private String errorPageTableSql = "";

	private String listHtmlTableName = "";
	private String listHtmlTableSql = "";
	private Configure conf = null;
	
	private void init(String configureName) {
		conf = new Configure(configureName);
		domain = conf.getDomain();
		listHtmlTableName = domain + "_html_list";
		urlTableName = domain + "_url";
		errorPageTableName = domain + "_error_page";
		initSql();
		initTable();
	}
	
	private void initSql() {
		this.pointersSql = FileReader.readFile("./sql/pointers.sql");
		this.urlTableSql = FileReader.readFile("./sql/url.sql");
//		this.listHtmlTableSql = FileReader.readFile("./sql/list_html.sql");
		this.errorPageTableSql = FileReader.readFile("./sql/error_page.sql");
	}

	private void initTable() {
		initOneTable(this.pointersTableName, this.pointersSql);
//		initOneTable(this.listHtmlTableName, this.listHtmlTableSql);
		initOneTable(this.urlTableName, this.urlTableSql);
		initOneTable(this.errorPageTableName, this.errorPageTableSql);
	}
	
	private void initOneTable(String table, String fields) {
		tableHelper=applicationContext.getBean(TableHelper.class);
		if (!tableHelper.isTableExist(table)) { // 表不存在即初始化

			tableHelper.createTable(table, fields);
			logger.info("create table " + table + " success!!!");
		}
	}

	public ListHtmlProcessor(Site site) {
		this.site = site;
	}
	

	@Override
	public void process(Page page) {

		urlGet(page);
		
//		String confname=site.getHeaders().get("confname");
//		//System.out.println(confname);
//		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
//				"classpath:/spring/applicationContext*.xml");
//		final UrlExtractor2 urlExtractor = applicationContext
//				.getBean(UrlExtractor2.class);
//		try {
//			urlExtractor.exec(confname);
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}

		
		/*
		ListHtml listHtml = new ListHtml();

		listHtml.setStatusCode(page.getStatusCode());
		String html = page.getRawText();
		listHtml.setHtml(html);

		listHtml.setCrawledTime(Utils.getNow());
		String url;// = page.getUrl().get();
		url = page.getRequest().getUrl();
		listHtml.setUrl(url);
		if (url != null) {
			listHtml.setUrlMd5(DigestUtils.md5Hex(url));
		}
		if (html != null) {
			listHtml.setPageMd5(DigestUtils.md5Hex(html));// 用于确定是已经抽过
		}
		listHtml.setType("List");
		
		// 存入扩展字段，后面持久化Pipeline调用
		page.putField("model", listHtml);
		*/
		
	}

	@Override
	public Site getSite() {
		return site;
	}
	
	public void urlGet(Page page){

		String confname=site.getHeaders().get("confname");

		this.init(confname);
		conf = new Configure(confname);
		
		Set<String> urlSet = new HashSet<String>();
		String error = null;
		String extractedTime = "";
		
		Extractor extractor = null;
		
		if ("URLPattern".equals(conf.getExtractMethod())) {
			extractor = new URLPatternExtractor(conf.getUrlPost());
		} else if ("CSSPath".equals(conf.getExtractMethod())) {
			extractor = new CSSPathExtractor(conf.getUrlPost());
		} else {
			logger.error("extract method error!!!");
		}
		
		SimpleDateFormat extractedTimeFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		extractedTime = extractedTimeFormat.format(date);
		
		Html html = page.getHtml();
		urlSet = extractor.extract(html);
		
//		String pageMd5 = DigestUtils.md5Hex(html);
		urlDao=applicationContext.getBean(URLDao.class);
		try {
			for (String url : urlSet) {
				try {
					urlDao.addURL(urlTableName, url, extractedTime);
				} catch (Exception e) {
					logger.error(e.toString());
					error = "500";
				}
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
		}

		urlSet.clear();
		
		
	}
}
